/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kom.gfx;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;
import kom.Bitmaps;
import kom.Block;
import kom.Border;
import kom.Effects.AnimatedEffect;
import kom.Map;
import kom.Player;

/**
 *
 * @author Nathan
 */
public class Screen {

    private Graphics2D g;
    private Bitmaps bitmaps;
    private int xoffs = 0, yoffs = 0;

    public Screen(Bitmaps bitmaps) throws IOException {
        this.bitmaps = bitmaps;
        bitmaps.loadAll();
    }

    public void drawEffect(AnimatedEffect e) {
        int x = (int) e.getX() - xoffs;
        int y = (int) e.getY() - yoffs;

        System.out.println(e.frame);
        
        g.drawImage(bitmaps.animatedEffects.get(e.type.id)[e.frame], x, y, null);
    }
    
    public void drawText(int x, int y, String text, Color c) {
        g.setColor(c);
        g.drawString(text, x, y);
    }

    public void drawMap(int w, int h, Block[][] map) {
        for (int i = 0; i < w * h; i++) {
            int xx = i % w * 8 - xoffs;
            int yy = i / h * 8 - yoffs;

            g.drawImage(bitmaps.getTileImage(map[i % w][i / h].getBlockType()), xx, yy, null);
        }
    }

    public void drawBorder(Border b) {
        int x = b.x * 8 - xoffs;
        int y = b.y * 8 - yoffs;

        g.drawImage(bitmaps.getTileImage(b.tileIndex, b.direction), x, y, null);
    }

    public void drawPlayer(Player p) {
        int x = (int) p.getX() - xoffs;
        int y = (int) p.getY() - yoffs - 2;

        if (p.getFacing() == Map.DIR_RIGHT) {
            g.drawImage(bitmaps.negativeSprites.get(p.getSprite())[p.getFrame()], x, y, null);
        } else if (p.getFacing() == Map.DIR_LEFT) {
            g.drawImage(bitmaps.sprites.get(p.getSprite())[p.getFrame()], x, y, null);
        }
    }

    public void drawRect(int x, int y, int w, int h, boolean fill, Color color) {
        g.setColor(color);
        if (fill) {
            g.fillRect(x, y, w, h);
        } else {
            g.drawRect(x, y, w, h);
        }
    }

    public void drawRect(Rectangle r, boolean fill, Color color) {
        g.setColor(color);
        if (fill) {
            g.fillRect(r.x, r.y, r.width, r.height);
        } else {
            g.drawRect(r.x, r.y, r.width, r.height);
        }
    }

    public Color getColor(int hex) {
        return new Color(hex);
    }

    public void setFont(String font, int style, int size) {
        g.setFont(new Font(font, style, size));
    }

    public void setGraphics(Graphics2D g) {
        this.g = g;
    }

    public Graphics2D getGraphics() {
        return g;
    }

    public void setOffs(int xoffs, int yoffs) {
        this.xoffs = xoffs;
        this.yoffs = yoffs;
    }

    public int getOffsX() {
        return xoffs;
    }

    public int getOffsY() {
        return yoffs;
    }
}
